﻿using DynamicViewFormLibrary.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DynamicViewFormLibrary
{

    public class RtfDomDocuments
    {
        private string template;
        private StringBuilder doc;


        /// <summary>
        /// Format Template File
        /// </summary>
        /// <param name="title">doc title</param>
        public void FormatTemplate(string title)
        {
            RtfDomParser parser = new RtfDomParser();

            doc = parser.FormatTemplate(template, title);

        }

        /// <summary>
        /// Load Template File
        /// </summary>
        public void LoadTemplate(string template)
        {
            this.template = template;

            //this.FormatTemplate();
        }

        public void Append(string content)
        {
            RtfDomParser parser = new RtfDomParser();
            doc.Append(parser.FormatContent(content));

        }

        public void Save(string fileName)
        {
            this.EndContent();

            byte[] bytes = Encoding.Default.GetBytes(doc.ToString());
            FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            fs.Write(bytes, 0, bytes.Length);
            fs.Flush();
            fs.Close();
        }

        /// <summary>
        /// Save Activity
        /// </summary>
        public void SaveActivity(string title)
        {
            string fileName = FilePathUtil.RTFWorkSpaceFilePath(title);
            this.EndContent();
            string desData = DESUtils.DESEncrypt(doc.ToString());//执行DES加密
            byte[] bytes = Encoding.Default.GetBytes(desData);
            FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            fs.Write(bytes, 0, bytes.Length);
            fs.Flush();
            fs.Close();
        }
        /// <summary>
        /// 插入结束标记
        /// </summary>
        private void EndContent()
        {
            doc.Append('}');
        }
        /// <summary>
        /// 加载文件
        /// </summary>
        /// <param name="fileName"></param>
        public List<StringBuilder> LoadFile(string fileName)
        {
            byte[] tb = File.ReadAllBytes(fileName);
            string doc = Encoding.Default.GetString(tb);
            doc = DESUtils.DESDecrypt(doc);

            int headerEndIndex = doc.IndexOf(@"\sectd");
            string header = doc.Substring(0, headerEndIndex);

            List<StringBuilder> list = new List<StringBuilder>();
            bool flat = false;
            while (flat != true)
            {
                int startIndex = doc.IndexOf(@"\rtbtabs");
                int endIndex = doc.IndexOf(@"\rtbtabe");
                if (startIndex != -1 && endIndex != -1)
                {
                    string s = doc.Substring(startIndex + @"\rtbtabs".Length, endIndex - startIndex - +@"\rtbtabe".Length);

                    StringBuilder b = new StringBuilder();
                    b.Append(@header);
                    b.Append(@s);
                    b.Append(@"}");

                    list.Add(b);

                    doc = doc.Substring(endIndex + @"\rtbtabe".Length);
                    Console.WriteLine(s);
                }
                else
                {
                    flat = true;
                }
            }
            return list;
        }
    }
}
